package com.example.api.service.impl;

import com.example.api.dao.CommodityDao;
import com.example.api.dao.InventoryDao;
import com.example.api.dao.InventoryRecordDao;
import com.example.api.dao.PurchaseDao;
import com.example.api.model.constants.LogisticsConstants;
import com.example.api.model.entity.Commodity;
import com.example.api.model.entity.Inventory;
import com.example.api.model.entity.InventoryRecord;
import com.example.api.model.entity.Purchase;
import com.example.api.model.support.RespBean;
import com.example.api.model.vo.CommodityChartVo;
import com.example.api.service.InventoryRecordService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class InventoryRecordServiceImpl implements InventoryRecordService {

    @Resource
    private InventoryDao inventoryDao;

    @Resource
    private InventoryRecordDao inventoryRecordDao;

    @Resource
    private CommodityDao commodityDao;

    @Resource
    private PurchaseDao purchaseDao;

    @Override
    public List<CommodityChartVo> analyzeCommodity(Integer type) {
        List<CommodityChartVo> result = Lists.newArrayList();
        List<InventoryRecord> all = inventoryRecordDao.findAllByType(type);
        Map<String, Integer> map = Maps.newHashMap();
        for (InventoryRecord record : all) {
            if (map.containsKey(record.getName())) {
                map.put(record.getName(), map.get(record.getName()) + record.getCount());
            } else {
                map.put(record.getName(), record.getCount());
            }
        }
        for (String key : map.keySet()) {
            result.add(new CommodityChartVo(map.get(key), key));
        }
        return result;
    }

    @Override
    public List<InventoryRecord> findAllByWarehouseId(Integer wid) {
        return inventoryRecordDao.findAllByWid(wid);
    }

    @Override
    public List<InventoryRecord> findAllByCommodityId(Integer cid) {
        return inventoryRecordDao.findAllByCid(cid);
    }

    @Override
    public RespBean out(InventoryRecord record) {
        //查找当前商品在该仓库的库存
        Inventory inventory = inventoryDao.findByWidAndCid(record.getWid(), record.getCid());
        //查询结果为空
        if (Objects.isNull(inventory)) {
            return RespBean.error("仓库不存在");
        }
        //比较库存
        if (inventory.getCount() < record.getCount()) {
            return RespBean.error("出库失败，库存数量不够");
        }
        Commodity commodity = commodityDao.selectByPrimaryKey(record.getCid());
        if (Objects.isNull(commodity)) {
            return RespBean.error("商品不存在");
        }
        commodity.setCount(commodity.getCount() - record.getCount());
        commodityDao.updateByPrimaryKeySelective(commodity);
        int result = inventory.getCount() - record.getCount();
        if (result == 0) {
            inventoryDao.deleteByWidAndCid(record.getWid(), record.getCid());
        }
        else {
            inventory.setCount(result);
            inventoryDao.updateByPrimaryKeySelective(inventory);
        }
        record.setCreateTime(new Date());
        record.setType(LogisticsConstants.INVENTORY_RECORD_TYPE_0);
        inventoryRecordDao.insertSelective(record);
        return RespBean.success("出库成功");
    }

    @Override
    public RespBean in(InventoryRecord record) {
        //更新待入库数量
        Purchase purchase = purchaseDao.selectByCid(record.getCid());
        if (Objects.isNull(purchase)) {
            return RespBean.error("待入库商品不存在");
        }
        if (purchase.getCount() < record.getCount()) {
            return RespBean.error("数量不足，入库失败");
        }
        int result = purchase.getCount() - record.getCount();
        if (result == 0) {
            purchaseDao.deleteByPrimaryKey(purchase.getId());
        }
        else {
            purchase.setCount(result);
            purchase.setUpdateTime(new Date());
            purchaseDao.updateByPrimaryKeySelective(purchase);
        }
        //更新商品数量
        Commodity commodity = commodityDao.selectByPrimaryKey(record.getCid());
        if (Objects.isNull(commodity)) {
            return RespBean.error("商品不存在");
        }
        commodity.setCount(commodity.getCount() + record.getCount());
        commodityDao.updateByPrimaryKeySelective(commodity);
        //查找当前商品在该仓库的库存
        Inventory inventory = inventoryDao.findByWidAndCid(record.getWid(), record.getCid());
        if (Objects.isNull(inventory)) {
            //新建该商品库存信息
            inventory = new Inventory();
            inventory.setCid(record.getCid());
            inventory.setWid(record.getWid());
            inventory.setCount(record.getCount());
            inventory.setName(record.getName());
            inventoryDao.insertSelective(inventory);
        }
        else {
            inventory.setCount(inventory.getCount() + record.getCount());
            inventoryDao.updateByPrimaryKeySelective(inventory);
        }
        record.setCreateTime(new Date());
        record.setType(LogisticsConstants.INVENTORY_RECORD_TYPE_1);
        inventoryRecordDao.insertSelective(record);
        return RespBean.success("入库成功");
    }

}